<?php

//------------------------------------------------------------------------------
// Class              : Ctl_Gioithieu
// Modification       :
//------------------------------------------------------------------------------
// Date(yyyy.mm.dd)   Developer                       Action
//------------------------------------------------------------------------------
// 2010.12.05         QuynhTTN                         Created
//------------------------------------------------------------------------------
// 2013.07.25         NguyenNgocManh                   Update
//------------------------------------------------------------------------------

class Ctl_Home extends ControlBase {

    var $modul = "info";

//==========================================================================
// Function    : Ctl_Gioithieu
// Creator     : QuynhTTN 2010.12.05
// Details     : Call the constructor of father class to initial variable
//==========================================================================
    function Ctl_Home() {
// Constructor of father class.
        ControlBase::ControlBase();
    }

//==========================================================================
// Function    : execute
// Argument    :
// ReturnValue :
// Creator     : QuynhTTN 2010.12.05
// Details     : Execute the process depend the given mode,
//               as: Add, Delete, Edit and default is List.
//==========================================================================
    function execute($action = "") {
        global $db;
        global $arrayError, $_POST;
        include_once DIR_MODEL . 'md_home.class.php';
        $objHome = new MD_Home($db);
        $arrayData = array();
// Get back the work you want to do.
        $this->modul = CM_getParameter(MODUL);
        if ($action == "") {
// Get back the action: as: Add, Delete, Edit or List.
            $action = CM_getParameter(ACTION);
        }

        switch ($action) {
            case "logout":
                $this->doLogOut();
                break;
            case "contacts":
                if (!isset($_POST['Submit'])) {
                    $arrayData = $objHome->getAll("*", " ORDER BY id");
                    include_once DIR_VIEW . 'v_contacts.php';
                    V_Contacts($arrayData, 'contacts');
                } else {
                    if ($this->update_contacts()) {
                        $mes1 = 'Cập nhật thành công!.';
                        echo "<script>alert('$mes1');</script>";
                        $url = "?" . MODUL . "=home&" . ACTION . "=contacts";
                    } else {
                        $mes2 = 'Cập nhật lỗi!.';
                        echo "<script>alert('$mes2');</script>";
                        $url = "?" . MODUL . "=home&" . ACTION . "=contacts";
                    }
                    CM_gotoPage($url);
                }
                break;
            case "banner":
                $this->do_update_banner();
                break;
            default :
                $this->do_update_about();
                break;
        }

        unset($arrayData);
    }

    function doLogOut() {
        session_unregister(ssLogin_Pkd);
        session_unset();
        CM_gotoPage("login.php");
    }

    function do_update_about() {
        global $db;
        $objHome = new MD_Home($db);
        if (isset($_POST['Submit'])) {
            if ($this->update_about()) {
                echo "<script>alert('Update thành công!);</script>";
                $url = "?" . MODUL . "=home&" . ACTION . "=about";
            } else {
                $url = "?" . MODUL . "=home&" . ACTION . "=about";
            }
            CM_gotoPage($url);
        } else {
            $arrayData = $objHome->getById2('8');
            include_once DIR_VIEW . 'v_about.php';
            V_About($arrayData, 'about');
        }
    }

    function update_about() {
        global $arrayError, $_POST, $db;
        $objHome = new MD_Home($db);
        $objHome->update_about(&$_POST);
        if ($objHome->errno()) {
            $arrayError[] = $objHome->error();
            return false;
        }
        return true;
    }

    function do_update_banner() {
        global $db;
        $objHome = new MD_Home($db);
        if (isset($_POST['Submit'])) {
            if ($this->update_banner()) {
                echo "<script>alert('Update thành công!);</script>";
                $url = "?" . MODUL . "=home&" . ACTION . "=banner";
            } else {
                $url = "?" . MODUL . "=home&" . ACTION . "=banner";
            }
            CM_gotoPage($url);
        } else {
            $arrayData = $objHome->getById2('3');
            include_once DIR_VIEW . 'v_about.php';
            V_About($arrayData, 'banner');
        }
    }

    function update_banner() {
        global $arrayError, $_POST, $db;
        $objHome = new MD_Home($db);
        $objHome->update_about(&$_POST);
        if ($objHome->errno()) {
            $arrayError[] = $objHome->error();
            return false;
        }
        return true;
    }

    function update_contacts() {
        global $arrayError, $_POST, $db;
        $objHome = new MD_Home($db);
        $objHome->update_contacts(&$_POST);
        if ($objHome->errno()) {
            $arrayError[] = $objHome->error();
            return false;
        }
        return true;
    }

}

?>